package com.ndp.fb.fbagent.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSONArray;
import com.ndp.fb.constant.CommonConstants;
import com.ndp.fb.fbagent.service.SerchFBService;
import com.ndp.fb.model.fbagent.param.serach.SearchParam;
import com.ndp.fb.model.fbagent.param.serach.SearchType;
import com.ndp.fb.model.fbagent.result.FBAdCountry;
import com.ndp.fb.model.fbagent.result.common.*;
import com.restfb.Parameter;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by infi on 2015/3/18.
 */
@Service
public class SerchFBServiceImpl extends AbstractFBagentService implements SerchFBService {
    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public List<?> search(SearchParam searchParam) {
        Class clazz = getSearchResultType(searchParam.getType());
        List<Parameter> parameterList = new ArrayList<>();
        if (StringUtils.isNoneBlank(searchParam.getLocale())) {
            parameterList.add(Parameter.with("locale", searchParam.getLocale()));
        }
        if (StringUtils.isNoneBlank(searchParam.getQ())) {
            parameterList.add(Parameter.with("q", searchParam.getQ()));
        }
        if (StringUtils.isNoneBlank(searchParam.getClassT())) {
            parameterList.add(Parameter.with("class", searchParam.getClassT()));
        }

        if (isNeedLimit(SearchType.getSearchType(searchParam.getType()))) {
            parameterList.add(Parameter.with("limit", CommonConstants.NUM_INT_500));
        }

        parameterList.add(Parameter.with("type", searchParam.getType().toLowerCase()));
        //覆盖掉不用的值
        return syncFetchList("/search", searchParam.getAccessToken(), parameterList, true, clazz);
    }

    @Override
    public List<?> search(String accessToken, String locale, String q, String searchType) {
        Class clazz = getSearchResultType(searchType);
        List<Parameter> parameterList = new ArrayList<>();
        parameterList.add(Parameter.with("locale", locale));
        parameterList.add(Parameter.with("q", q));
        parameterList.add(Parameter.with("type", searchType.toLowerCase()));
        return syncFetchList("/search", accessToken, parameterList, true, clazz);
    }

    @Override
    public List<FBAdInterest> searchInterestList(String accessToken, String locale, String q, List<String> interestList) {
        List<Parameter> parameterList = new ArrayList<>();
        parameterList.add(Parameter.with("type", "adinterestvalid"));
        parameterList.add(Parameter.with("locale", locale));
        parameterList.add(Parameter.with("interest_list", JSONArray.toJSON(interestList)));
        return syncFetchList("/search", accessToken, parameterList, true, FBAdInterest.class);
    }
    @Override
    public List<FBAdInterest> searchInterestListForValid(String accessToken, List<Long> ids) {
        List<Parameter> parameterList = new ArrayList<>();
        parameterList.add(Parameter.with("type", "adinterestvalid"));
        parameterList.add(Parameter.with("interest_fbid_list", JSONArray.toJSON(ids)));
        return syncFetchList("/search", accessToken, parameterList, true, FBAdInterest.class);
    }

    private Class getSearchResultType(String type) {
        Class clazz;
        if (SearchType.ADCOUNTRY.getType().equals(type)) {
            clazz = FBAdCountry.class;
        } else if ((SearchType.ADINTEREST.getType().equals(type)) || (SearchType.ADINTERESTCATEGORY.getType().equals(type))) {
            clazz = FBAdInterest.class;
        } else if (SearchType.ADLOCALE.getType().equals(type)) {
            clazz = FBAdLocale.class;
        } else if (SearchType.ADTARGETINGCATEGORY.getType().equals(type)) {
            clazz = FBBehaviors.class;
        } else {
            clazz = String.class;
            logger.info("-------------SearchParam 传入的type不在系统定义枚举内");
        }
        return clazz;
    }

    private boolean isNeedLimit(SearchType type) {
        switch (type) {
            case ADCOUNTRY:
            case ADLOCALE:
                return true;
            case ADINTEREST:
            case ADINTERESTCATEGORY:
            case ADTARGETINGCATEGORY:
                return false;
            default:
                return true;
        }
    }

    @Override
    public List<FBAdInterest> searchInterestList(String accessToken, String en_us, List<String> waitFilterInterests) {
        return null;
    }

    @Override
    public List<FBAdGeolocation> searchCitys(String accessToken, String locale, String q) {
        try {
            List<Parameter> parameterList = new ArrayList<>();
            parameterList.add(Parameter.with("q", q));
            parameterList.add(Parameter.with("location_types", "['city']"));
            parameterList.add(Parameter.with("locale", locale));
            parameterList.add(Parameter.with("type", "adgeolocation"));
            return syncFetchList("/search", accessToken, parameterList, true, FBAdGeolocation.class);
        } catch (Exception e) {
            logger.error("查找出错>>", e);
            throw e;
        }
    }

    @Override
    public <T> List<T> search(SearchParam searchParam, Class<T> resultClass) {
        List<Parameter> parameterList = adapter(searchParam);
        boolean isall=searchParam.getLimit()==null?true:false;
        return syncFetchList("/search", searchParam.getAccessToken(), parameterList, isall, resultClass);
    }

	@Override
	public List<FBAdGeolocation> searchAllGeolocation(String accessToken, String locale, String q) {
		 try {
	            List<Parameter> parameterList = new ArrayList<>();
	            parameterList.add(Parameter.with("q", q));
	            parameterList.add(Parameter.with("locale", locale));
	            parameterList.add(Parameter.with("type", "adgeolocation"));
	            return syncFetchList("/search", accessToken, parameterList, false,0,10, FBAdGeolocation.class);
	        } catch (Exception e) {
	            logger.error("查找出错>>", e);
	            throw e;
	        }
	}
}
